<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Tongbutui
 *
 * @author sunday 2014.05.26 add  modify by dragon.hu @2016.3.2
 */
require_once 'BaseChannel.php';

//同步平台服务器端接入
class Tongbutui implements BaseChannel {
	
	private $paramArray;	//构造参数数组
	
    function __construct() {
		//构造参数数组
		$this->paramArray = array(
		                    'source'=>'',
		                    'trade_no'=>'',
		                    'amount'=>'',
		                    'partner'=>'',
		                    'paydes'=>'',//SDK2.4新增字段，传支付说明信息
		                    'debug'=>0,//是否是测试模式
							'tborder'=>'',//同步平台订单号
		                    'sign'=>'',
		              );
    }

    function android($data) 
    {
    	$gamekey = '';//游戏验证key 由同步分配
    	echo $this->payCallBackMethod($gamekey, ANDROID);
    }

    function ios($data) 
    {
    	$gamekey = 'JMSjpHtzaHIW6SORmhX0g*0*2xcqw*5T';//游戏验证key 由同步分配
        echo $this->payCallBackMethod($gamekey, IOS);
    }
    
    //$platform 平台 名称
    function payCallBackMethod($gamekey, $platform)
    {   
    	$orderLog = new ChargeOrder();
    	$orderLog->log("tongbutui", $_REQUEST);
    	 	            
		//参数赋值
		foreach($this->paramArray as $key =>$data)
		{
		    if(isset($_REQUEST[$key]))
		    {
		        $this->paramArray[$key] = $_REQUEST[$key];
		    }
		}
		$str= 'source='.$this->paramArray['source'].'&trade_no='.$this->paramArray['trade_no'].'&amount='.$this->paramArray['amount']
				.'&partner='.$this->paramArray['partner'];
				
		//生成 sign 加密串 参数顺序要保持一致
		if(isset($_REQUEST['paydes']) && isset($_REQUEST['debug']))
		{//SDK 2.4 版本以上(含2.4)paydes 需加入验证 
		    $str = $str.'&paydes='.$this->paramArray['paydes'].'&debug='.$this->paramArray['debug'];
		}
		if(isset($_REQUEST['tborder']))
		{//sdk 3.1版本（含3.1）tborder需加入验证
			$str = $str.'&tborder='.$this->paramArray['tborder'];
		}
		$str .= '&key='.$gamekey;
		
		$sign = md5($str);
		//echo " str:$str \r\n";
		//echo " createsign:$sign getsign:".$this->paramArray['sign']."\r\n";
		
		if($sign==$this->paramArray['sign'])
		//if(1==1)	//for test
		{
			//注意，充值金额为分，在此需要除100
			if(true == $orderLog->recordOrder(STATUS_SUCCESS, $this->paramArray['amount']/100, $this->paramArray['trade_no'], "IosTongBu", $platform,$this->paramArray['tborder']))
			{
				$orderLog->log("tongbutui", " money:".$this->paramArray['amount']." order:". $this->paramArray['trade_no']." success");
		    	return json_encode(array('status'=>'success'));
			}else 
			{
				$orderLog->log("tongbutui"," money:".$this->paramArray['amount']." order:". $this->paramArray['trade_no']." insert database error ");
				return json_encode(array('status'=>'error'));
			}
		}
		else
		{
			$orderLog->log("tongbutui"," order:".$this->paramArray['trade_no']." sign error ");
		    return json_encode(array('status'=>'error'));
		}
    }
 
}